<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script type="text/javascript" src="${ctxStatic}/jslib/aes.js"></script>
<script type="text/javascript" src="${ctxStatic}/jslib/pad-zeropadding.js"></script>	
<script type="text/javascript" src="${ctxStatic}/jquery/base64.min.js"></script>
<%@ attribute name="formid" type="java.lang.String" required="true" description="formid"%>		
<%@ attribute name="passwordId" type="java.lang.String" required="true" description="passwordId"%>
<%@ attribute name="aeskeyId" type="java.lang.String" required="true" description="passwordId"%>
<input type="text" id="${aeskeyId}" name="${aeskeyId}" class="txt required" value="" style="display:none"/>
<script type="text/javascript">
	String.prototype.reverse = function() {
		var s = "";
		var i = this.length;
		while (i>0) {
			s += this.substring(i-1,i);
			i--;
		}
		return s;
	}
    function cryptoJS(data,keyval)
    {
		var key  = CryptoJS.enc.Latin1.parse(keyval);
		var iv   = CryptoJS.enc.Latin1.parse(keyval.reverse());
		var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
		return encrypted;
    };
</script>
<script type="text/javascript">
	$(document).ready(function() {
		var url="${pageContext.request.contextPath}/servlet/AESKeyServlet?v="+<%=(int)(Math.random()*1000000000)%>;;
		$.ajax({url: url, dataType: 'text'})
		.done(function(result) {
			$("#${aeskeyId}").val(result);
		})
		.fail(function() {
		});
		$('#${formid}').submit(function(){
			var password=$('#${passwordId}').val();
			var keyval=$('#${aeskeyId}').val();
			//var encryptedPW = cryptoJS(password,keyval);
			var encryptedPW = Base64.encode(password);
			//console.log(encryptedPW);
			//alert(encryptedPW);
			$('#${passwordId}').val(encryptedPW);
			return true;
		});
	});
</script>
